package photobox.manage

import grails.converters.JSON
import photobox.BaseController
import photobox.exception.NotFoundException
import photobox.utils.ParamUtils
import photobox.utils.ResultUtil
import photobox.photo.PhotoSize

class MngPhotoSizeController extends BaseController {

    def  photoSizeService
    def index() {}
    def create() {
    }
    def list() {
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id", "width", "height","packingPrice"], [sort: 'id', order: "desc"])
        def userRs = photoSizeService.listPhotoSize(searchParams, pageParams, sortParams)
        render ResultUtil.pageResults(userRs.rs, userRs.total, pageParams,
                [id: 'id', width: 'width', height: 'height',packingPrice:'packingPrice']) as JSON
    }
    def save() {
        photoSizeService.createPhotoSize(params)
        render([error: 0, message: "创建图片尺寸完成"] as JSON)
    }
    def edit() {
        def photoSize = PhotoSize.get(params.id as Long)
        if (!photoSize) {
            throw new NotFoundException("没有找到对应的图片尺寸")
        }
        return [photoSize: photoSize]
    }

    def update() {
        photoSizeService.updatePhotoSize(params)
        render([error: 0, message: "更新图片尺寸完成"] as JSON)
    }

    def delete() {
        photoSizeService.deletePhotoSize(params)
        render([error:0, message: "删除图片尺寸完成"] as JSON)
    }
}
